package com.yuanzhi.train;

import java.util.*;

/**
 * 给你一个数组 items ，其中 items[i] = [typei, colori, namei] ，描述第 i 件物品的类型、颜色以及名称。
 * 另给你一条由两个字符串 ruleKey 和 ruleValue 表示的检索规则。 如果第 i 件物品能满足下述条件之一，则认为该物品与给定的检索规则 匹配
 * ：
 * <p>
 * ruleKey == "type" 且 ruleValue == typei 。 ruleKey == "color" 且 ruleValue ==
 * colori 。 ruleKey == "name" 且 ruleValue == namei 。 统计并返回 匹配检索规则的物品数量 。
 * <p>
 * 示例 1：
 * <p>
 * 输入：items =
 * [["phone","blue","pixel"],["computer","silver","lenovo"],["phone","gold","iphone"]],
 * ruleKey = "color", ruleValue = "silver" 输出：1 解释：只有一件物品匹配检索规则，这件物品是
 * ["computer","silver","lenovo"] 。 示例 2：
 * <p>
 * 输入：items =
 * [["phone","blue","pixel"],["computer","silver","phone"],["phone","gold","iphone"]],
 * ruleKey = "type", ruleValue = "phone" 输出：2 解释：只有两件物品匹配检索规则，这两件物品分别是
 * ["phone","blue","pixel"] 和 ["phone","gold","iphone"]
 * 。注意，["computer","silver","phone"] 未匹配检索规则。   提示： 1 <= items.length <= 104 1
 * <= typei.length, colori.length, namei.length, ruleValue.length <= 10 ruleKey
 * 等于 "type"、"color" 或 "name" 所有字符串仅由小写字母组成
 *
 * @author zhangyuanzhi
 * @since 2021/3/3 11:20
 */
public class CountMatches {
	public static int countMatches(List<List<String>> items, String ruleKey,
			String ruleValue) {
		int matchNum = 0;
		for (List<String> itemList : items) {
			switch (ruleKey) {
				case "type":
					if (Objects.equals(ruleValue, itemList.get(0))) {
						matchNum++;
					}
					break;
				case "color":
					if (Objects.equals(ruleValue, itemList.get(1))) {
						matchNum++;
					}
					break;
				case "name":
					if (Objects.equals(ruleValue, itemList.get(2))) {
						matchNum++;
					}
					break;
				default:
					throw new IllegalArgumentException("unknown ruleKey!");
			}
		}
		return matchNum;
	}

	public static int countMatchesImprove(List<List<String>> items,
			String ruleKey, String ruleValue) {
		Map<String, Integer> dicMap = new HashMap<>(3);
		dicMap.put("type", 0);
		dicMap.put("color", 1);
		dicMap.put("name", 2);
		int matchNum = 0;
		for (List<String> itemList : items) {
			if (itemList.get(dicMap.get(ruleKey)).equals(ruleValue)) {
				matchNum++;
			}
		}
		return matchNum;
	}

	public static void main(String[] args) {
		List<String> items1 = Arrays.asList("phone", "blue", "pixel");
		List<String> items2 = Arrays.asList("computer", "silver", "phone");
		List<String> items3 = Arrays.asList("phone", "gold", "iphone");
		List<List<String>> items = Arrays.asList(items1, items2, items3);
		System.out.println(countMatches(items, "type", "phone"));
	}
}
